<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script>
    var webSocket = null;
    if('WebSocket' in window){
        webSocket = new WebSocket('ws://localhost:8080/webSocket')
    }else{
        alert('该浏览器不支持消息通知');
    }
    webSocket.onopen = function (event) {
        console.log('建立链接');
    }
    
    webSocket.onclose = function (event) {
        console.log('链接关闭');
    }
    
    webSocket.onmessage = function (event) {
        console.log('收到消息'+event.data);
        alert('收到消息'+event.data);
    }
    
    webSocket.onerror = function () {
        alert('接收消息出现错误');
    }

    window.onbeforeunload = function () {
        console.log('销毁链接');
        webSocket.close();
    }
</script>
</html>